<template>
    <div>
        <HeaderS :styleColor="1" />
        <div class="page-content">
            <div class="main">
                <div :class="screenWidth > 1000 ? 'bread-crumb-box' : 'h5s'">
                    <div class="width-1360">
                        <img src="@/assets/img/home.png" class="home-icon">
                        <span>当前位置：</span>
                        <span>
                            <router-link to="/">&nbsp;首页</router-link>
                            <img src="@/assets/img/right.png" class="left-icon">
                        </span>
                        <span>
                            <span class="now">美食推荐</span>
                        </span>
                    </div>
                </div>
                <div :class="screenWidth > 1000 ? 'main-wrap' : 'h5s'">
                    <div v-if="screenWidth > 1000" class="title-wrap">
                        <div class="title">美食推荐</div>
                    </div>
                    <div class="nav-wrap">
                        <div v-if="typeList.length > 0" @click="changeType(0)"
                            :class="{ 'nav-list': true, 'active': type == 0 }"> 全部 </div>
                        <div v-for="(item, index) in typeList" :key="index" @click="changeType(item.id)"
                            :class="{ 'nav-list': true, 'active': type == item.id }"> {{ item.name }} </div>
                    </div>
                    <div class="list-wrap" v-loading="loading">
                        <div v-for="(item, index) in list" :key="item.id" @click="getOpen(item.id, item.url)"
                            :class="{ 'list-box': true, 'marginShow': isHighlight(index) }">
                            <div class="img-box" :style="{ backgroundImage: 'url(' + item.thumbnail + ')' }">
                            </div>
                            <div class="text-wrap">
                                <div class="name line1"> {{ item.post_title }} </div>
                            </div>
                        </div>
                        <Nodata v-if="list.length < 1"></Nodata>
                    </div><!----><!---->
                    <div class="width-1360" v-if="screenWidth > 1000">
                        <div class="paging_box" v-if="totalPage > 1">
                            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                :current-page="currentPage1" :page-size="pageSize" layout='total,slot, pager'
                                :total="total">
                                <el-button :disabled="currentPage1 == 1" @click="currentPage1 = 1" type="button"
                                    class="first-pager"><!----><!----><span> 首页
                                    </span>
                                </el-button>
                                <el-button :disabled="currentPage1 == 1" @click="currentPage1 = currentPage1 - 1"
                                    type="button" class="first-pager"><!----><!----><span> 上一页
                                    </span>
                                </el-button>
                            </el-pagination>
                            <div class="paging_box2">
                                <el-pagination layout='slot'>
                                    <el-button :disabled="currentPage1 == totalPage"
                                        @click="currentPage1 = currentPage1 + 1" type="button"
                                        class="first-pager"><!----><!----><span> 下一页
                                        </span>
                                    </el-button>
                                    <el-button :disabled="currentPage1 == totalPage" @click="currentPage1 = totalPage"
                                        type="button" class="first-pager"><!----><!----><span> 尾页
                                        </span>
                                    </el-button>

                                </el-pagination>
                            </div>
                            <div class="jumper-box">
                                跳转到第
                                <input type="number" v-model="jumperVal" @input="change" class="jumper-page">
                                页
                                <span @click="currentPage1 = jumperVal" class="jumper-go"> GO </span>
                            </div>
                        </div>

                    </div>
                    <div v-else class="btn">
                        <div v-if="totalPage > 1" @click="changePage('prev')"
                            :class="currentPage1 == 1 ? 'prev-no' : 'prev'">上一页</div>
                        <div v-if="totalPage > 1" @click="changePage('next')"
                            :class="currentPage1 == totalPage ? 'next-no' : 'next'">下一页</div>
                    </div>
                </div>
            </div>
        </div>
        <FooterS />
    </div>
</template>
<script>

export default ({
    data() {
        return {
            currentPage1: 1,
            total: 0,
            pageSize: 6,
            totalPage: 0,
            list: [],
            jumperVal: 1,
            typeList: [],
            type: 0,
            loading: false
        }
    },
    computed: {
        screenWidth() {
            return this.$store.getters.screenWidth
        }
    },
    watch: {

        currentPage1(n, o) {
            window.scrollTo({ top: 0, behavior: 'smooth' });
            if (n && this.$route.query.page != n) {
                this.$router.push({ query: { page: n, tag: this.type ? this.type : '' } })
            }
            if (this.type != 0) {
                this.getData({ page: n, categories: this.type })
            } else {
                this.getData({ page: n })
            }
        }
    },
    created() {
        window.scrollTo({ top: 0});
        if (this.$route.query.tag) {
            this.type = this.$route.query.tag
        }
        if (this.$route.query.page) {
            if (this.currentPage1 != this.$route.query.page) {
                this.currentPage1 = parseInt(this.$route.query.page)
                return
            }
        }
        this.getData({
            page: this.currentPage1,
            categories: this.type
        })
    },
    methods: {
        getOpen(id, url) {
            if (url) {
                this.$confirm('', '温馨提示', {
                    message: this.$store.state.cnode,
                    dangerouslyUseHTMLString: true,
                    confirmButtonText: '继续访问',
                    cancelButtonText: '放弃',
                    center: true
                })
                    .then(_ => {
                        window.open(url, '_blank');
                    })
                    .catch(_ => { })
            } else {
                this.$router.push(`/food-details?id=${id}`)
            }
        },
        isHighlight(index) {
            return (index + 1) % 3 === 2; // 3n-1 等价于 (index + 1) % 3 === 2
        },
        getData(data) {
            this.loading = true;
            this.$api.getFoodList({ lang: this.$i18n.locale, ...data }).then((res) => {
                this.loading = false
                if (res.data.code == 1) {
                    this.typeList = res.data.data.categories
                    this.list = res.data.data.list_data.data
                    this.total = res.data.data.list_data.total
                    this.pageSize = res.data.data.list_data.per_page
                    this.totalPage = res.data.data.list_data.last_page

                }
            })
        },
        changeType(val) {
            if (this.currentPage1 == 1) {
                if (this.type != val) {
                    if (val != 0) {
                        this.getData({ categories: val })
                    } else {
                        this.getData()
                    }
                    this.type = val
                    this.$router.push({ query: { page: this.currentPage1, tag: val ? val : '' } })
                } else {
                    this.getData({ categories: val })
                }
            } else {
                this.currentPage1 = 1
                this.type = val
                this.$router.push({ query: { page: this.currentPage1, tag: val ? val : '' } })
            }

        },
        changePage(num) {
            if (num == 'prev') {
                this.currentPage1 = Math.max(1, this.currentPage1 - 1)
            } else {
                this.currentPage1 = Math.min(this.totalPage, this.currentPage1 + 1)
            }
        },
        change() {
            if (this.jumperVal > this.totalPage) {
                this.jumperVal = this.totalPage;
            }
            if (this.jumperVal < 1) {
                this.jumperVal = 1;
            }
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.currentPage1 = val
        },
    }
})
</script>
<style lang="scss" scoped>
.bread-crumb-box {
    ::v-deep span {
        a:hover {
            color: #e31c1c;
        }
    }
}

.paging_box {
    ::v-deep .el-pagination {
        white-space: nowrap;
        padding: 0.10417vw 0.26042vw;
        color: #303133;
        font-weight: 700;
    }

    ::v-deep .el-pager li:hover {
        color: #e31c1c;
    }

    ::v-deep .el-pager li {
        padding: 0 0.20833vw;
        background: #FFF;
        font-size: 0.67708vw;
        min-width: 1.84896vw;
        height: 1.45833vw;
        line-height: 1.45833vw;
        box-sizing: border-box;
        text-align: center;
    }

    ::v-deep .el-pagination button,
    ::v-deep .el-pagination span:not([class*=suffix]) {
        display: inline-block;
        font-size: 0.67708vw;
        min-width: 1.84896vw;
        height: 1.45833vw;
        line-height: 1.45833vw;
        vertical-align: top;
        box-sizing: border-box;
    }

    ::v-deep .el-pagination__total {
        margin-right: 0.52083vw;
        font-weight: 400;
        color: #606266;
    }

    ::v-deep .el-pagination .btn-next,
    ::v-deep .el-pagination .btn-prev {
        background: center center no-repeat #FFF;
        background-size: 0.83333vw;
        cursor: pointer;
        margin: 0;
        color: #303133;
    }

    ::v-deep .el-pagination .btn-prev {
        padding-right: 0.625vw;
    }

    ::v-deep .el-select-dropdown__item.selected,
    ::v-deep .el-pager li.active,
    ::v-deep .el-pagination button:hover {
        color: #e31c1c;
    }

    ::v-deep .el-pagination button[disabled]:hover {
        color: #C0C4CC;
    }
}

.paging_box {
    width: 100%;
    display: -webkit-box;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
}

.jumper-box {
    font-size: 0.72917vw;
    color: #666;
    display: -webkit-box;
    display: flex;
    padding: 0.10417vw 0.26042vw;
    line-height: 1.45833vw;
}

.jumper-box .jumper-page {
    width: 2.08333vw;
    height: 1.45833vw;
    line-height: 1.45833vw;
    text-align: center;
    margin: 0 0.3125vw;
    padding: 0 0.20833vw;
    background-color: #fff;
    border-radius: 0.20833vw;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: 0.67708vw;
    outline: none;

    -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", STHeiti, "Microsoft Yahei", Tahoma, Simsun, sans-serif;
}

.jumper-box .jumper-page::-webkit-inner-spin-button,
.jumper-box .jumper-page::-webkit-outer-spin-button {
    appearance: none;
    -webkiet-appearance: none;
}

.jumper-box .jumper-go {
    display: block;
    width: 1.5625vw;
    height: 1.14583vw;
    line-height: 1.14583vw;
    border-radius: 0.10417vw;
    background-color: #f5f5f5;
    border: 1px solid #999;
    font-size: 0.625vw;
    text-align: center;
    color: #666;
    margin: 0.15625vw 0 0 0.83333vw;
    cursor: pointer;
}

.first-pager,
.last-pager {
    padding: 0 0;
    font-size: 0.67708vw;
    min-width: 1.84896vw;
    height: 1.45833vw;
    line-height: 1.45833vw;
    box-sizing: border-box;
    text-align: center;
    color: #666;
    font-weight: 700;
    margin: 0 0.52083vw;
    background: 0 0;
    border: none;
}

.paging_box2 {
    ::v-deep .el-pagination {
        padding: 0.10417vw 0;
    }
}

.bread-crumb-box {
    padding: 2.60417vw 0 3.125vw;
}

.bread-crumb-box .width-1360 {
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
    align-items: center;
}

.width-1360 {
    width: 70.83333vw;
    margin: 0 auto;
}

.main {
    padding-top: 3.64583vw;
}

.bread-crumb-box .home-icon {
    width: 0.88542vw;
    line-height: 1;
    color: #999;
    margin-right: 0.52083vw;
    display: inline-block;
    vertical-align: middle;
}

.bread-crumb-box span {
    display: inline-block;
    vertical-align: middle;
    font-size: 0.72917vw;
    font-weight: 400;
    color: #666;
    line-height: 0.9375vw;
}

.bread-crumb-box span a {
    color: #666;
}

.bread-crumb-box .left-icon {
    color: #666;
    margin: 0 0.52083vw;
    display: inline-block;
    vertical-align: middle;
}

.bread-crumb-box span:nth-last-of-type(1) {
    color: #333;
}

.bread-crumb-box span .now {
    color: #e31c1c
}

.main-wrap {
    width: 100%;
    padding: 0 0 5.20833vw;
}

.main-wrap .title-wrap {
    width: 70.83333vw;
    margin: 0 auto 2.03125vw;
}

.main-wrap .title-wrap .title {
    color: #333;
    font-size: 1.5625vw;
    font-weight: bold;
    margin-bottom: 0.52083vw;
}

.main-wrap .title-wrap .summary {
    font-size: 0.83333vw;
    color: #666;
}

.main-wrap .list-wrap {
    width: 70.83333vw;
    margin: 0 auto 1.04167vw;
    min-height: 22vw;
}

.main-wrap .list-wrap .list-box {
    width: 22.1875vw;
    display: inline-block;
    margin-bottom: 2.08333vw;
    cursor: pointer;
}

.main-wrap .list-wrap .list-box .img-box {
    width: 22.1875vw;
    height: 12.5vw;
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
}


.main-wrap .list-wrap .marginShow {
    margin-left: 2.08333vw;
    margin-right: 2.08333vw;
}

.main-wrap .list-wrap .list-box .text-wrap {
    width: 100%;
    background: #fff;
    border: solid 1px #eeeeee;
    padding: 1.04167vw 1.5625vw;
}

.main .main-wrap .list-wrap .list-box .text-wrap .name {
    font-size: 0.83333vw;
    font-weight: bold;
    color: #333;
}

.main-wrap .nav-wrap {
    display: -webkit-box;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    margin-bottom: 2.03125vw;
}

.main-wrap .nav-wrap .nav-list {
    min-width: 8.33333vw;
    padding: 0 0.52083vw;
    height: 2.08333vw;
    line-height: 1.875vw;
    border-radius: 1.04167vw;
    border: solid 1px #d8d8d8;
    font-size: 0.83333vw;
    color: #666;
    margin: 0 0.52083vw;
    text-align: center;
    cursor: pointer;
}

.main-wrap .nav-wrap .active {
    color: #fff;
    background: #e31c1c;
    border-color: #e31c1c;
}

.main-wrap .nav-wrap .nav-list:hover {
    color: #fff;
    background: #e31c1c;
    border-color: #e31c1c;
}

.main .main-wrap .list-wrap .list-box:hover .name {
    color: #e31c1c;
}

@media(max-width:1000px) {
    .h5s-content {
        min-height: -webkit-calc(100vh - 6rem);
        min-height: calc(100vh - 6rem);
    }

    .h5s {
        font-size: 0;
        padding: 0.5rem 0;
    }

    .h5s .width-1360 {
        width: 100%;
        padding: 0 0.3rem;
        font-size: 0.2rem;
        color: #666;
        display: -webkit-box;
        display: flex;
        -webkit-box-align: center;
        align-items: center;
    }

    .h5s .width-1360 .home-icon {
        width: 0.22rem;
        margin-right: 0.1rem;
    }

    .h5s .width-1360 a {
        color: #666;
    }

    .h5s .width-1360 .left-icon {
        margin: 0 0.1rem;
    }

    .h5s .width-1360 .now {
        color: #e31c1c;
    }

    .h5s .list-wrap {
        padding: 0 0.3rem;
    }

    .h5s .list-wrap .list-box {
        margin-bottom: 0.6rem;
        margin: 0 0 0.6rem;
    }

    .h5s .list-wrap .list-box .img-box {
        width: 6.91rem;
        height: 3.83rem;
        position: relative;
        background-size: cover;
        background-position: center center;
        background-repeat: no-repeat;
    }

    .h5s .list-wrap .list-box .img-box img {
        width: 100%;
        height: 100%;
        -o-object-fit: cover;
        object-fit: cover;
    }

    .h5s .list-wrap .list-box .text-wrap {
        background: #f5f5f5;
        width: 100%;
        height: 1rem;
        font-weight: 700;
        font-size: 0.3rem;
        color: #333;
        text-align: center;
        line-height: 1rem;
    }

    .main .h5s:nth-child(2) {
        padding: 0;
    }

    .main {
        padding: 0;
    }

    .btn {
        display: -webkit-box;
        display: flex;
        -webkit-box-pack: center;
        justify-content: center;
        padding: 0.2rem 0 1rem;
    }

    .btn div {
        width: 1.6rem;
        height: 0.6rem;
        background-color: #f5f5f5;
        border-radius: 0.3rem;
        text-align: center;
        line-height: 0.6rem;
        font-size: 0.24rem;
        color: #666;
        margin: 0 0.15rem;
    }

    .btn div {
        width: 1.6rem;
        height: 0.6rem;
        background-color: #f5f5f5;
        border-radius: 0.3rem;
        text-align: center;
        line-height: 0.6rem;
        font-size: 0.24rem;
        color: #666;
        margin: 0 0.15rem;
        cursor: pointer;
    }

    .prev-no,
    .next-no {
        color: #999 !important;
    }

    .h5s .nav-wrap {
        display: -webkit-box;
        display: flex;
        flex-wrap: wrap;
        -webkit-box-pack: center;
        justify-content: center;
        margin-bottom: 0.4rem;
    }

    .h5s .nav-wrap .nav-list {
        min-width: 1.8rem;
        padding: 0 0.1rem;
        height: 0.4rem;
        line-height: 0.36rem;
        border-radius: 0.2rem;
        border: solid 1px #d8d8d8;
        font-size: 0.2rem;
        color: #666;
        margin: 0 0.1rem 0.2rem;
        text-align: center;
        cursor: pointer;
    }

    .h5s .nav-wrap .active {
        color: #fff;
        background: #e31c1c;
        border-color: #e31c1c;
    }

}
</style>